<template>
  <div class="app-container">

    <list-page-container>
      <!--搜索区-->
      <template slot="query">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
          <el-form-item label="企业名称" prop="enterpriseName">
            <el-input
                v-model="queryParams.enterpriseName"
                placeholder="请输入企业名称"
                clearable
                style="width: 240px"
                @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="企业代码" prop="enterpriseCode">
            <el-input
                v-model="queryParams.enterpriseCode"
                placeholder="请输入企业代码"
                clearable
                style="width: 240px"
                @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="企业类型" prop="enterpriseType">
            <el-select v-model="queryParams.enterpriseType" placeholder="企业类型" clearable>
              <el-option
                  v-for="dict in dict.type.sys_enterprise_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </template>
      <!--功能按钮区-->
      <template slot="handel">
        <el-row class="handle-container" :gutter="10">
          <el-col :span="1.5">
            <el-button
                type="primary"
                plain
                icon="el-icon-plus"
                size="mini"
                @click="handleAdd"
            >新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
                type="danger"
                plain
                icon="el-icon-delete"
                size="mini"
                :disabled="multiple"
                @click="handleDelete"
            >删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
                type="warning"
                plain
                icon="el-icon-download"
                size="mini"
                @click="handleExport"
            >导出</el-button>
          </el-col>
        </el-row>
      </template>
      <!--列表区-->
      <template slot="table">
        <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="企业名称" align="center" prop="enterpriseName" />
          <el-table-column label="企业代码" align="center" prop="enterpriseCode" />
          <el-table-column label="企业类型" align="center" prop="enterpriseType" >
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sys_enterprise_type" :value="scope.row.enterpriseType"/>
            </template>
          </el-table-column>
          <el-table-column label="企业地址" align="center" prop="enterpriseAddress" />
          <el-table-column label="管理员姓名" align="center" prop="adminUserName" />
          <el-table-column label="管理员帐号" align="center" prop="adminUserPhone" />
          <el-table-column width="300" label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"
              >修改</el-button>
              <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row)"
              >删除</el-button>
              <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-plus"
                  @click="addAdmin(scope.row)"
                  v-if="scope.row.adminUserSerial == null || scope.row.adminUserSerial === ''"
              >新增管理员</el-button>
              <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="alterPassword(scope.row)"
                  v-if="scope.row.adminUserSerial != null && scope.row.adminUserSerial !== ''"
              >修改密码</el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <!--分页区-->
      <template slot="pageDisplay">
        <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
        />
      </template>
    </list-page-container>

    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="企业名称" prop="enterpriseName">
          <el-input v-model="form.enterpriseName" placeholder="请输入企业名称" />
        </el-form-item>
        <el-form-item label="企业代码" prop="enterpriseCode">
          <el-input v-model="form.enterpriseCode" placeholder="请输入字典类型" />
        </el-form-item>
        <el-form-item label="企业类型" prop="enterpriseType">
          <el-select v-model="form.enterpriseType">
            <el-option
                v-for="dict in dict.type.sys_enterprise_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="企业地址" prop="enterpriseAddress">
          <el-input type="textarea"  v-model="form.enterpriseAddress" placeholder="请输入企业地址" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!--新增管理员会话框-->
    <el-dialog :title="title" :visible.sync="addAdminShow"  width="700px" append-to-body>
      <!--ref值为自己定义的组件名--><!--rules的值为校验规则配置对象-->
      <el-form ref="enterpriseAdminInfo" :model="enterpriseAdminInfo" :rules="enterpriseAdminInfoRules" label-width="140px">
        <el-form-item label="管理人员姓名" prop="name"><!--prop值为要校验的表单中的对应的属性-->
          <el-input v-model="enterpriseAdminInfo.name" placeholder="请输入管理人员姓名" />
        </el-form-item>
        <el-form-item label="管理人员手机号" prop="phone"><!--prop值为要校验的表单中的对应的属性-->
          <el-input v-model="enterpriseAdminInfo.phone" placeholder="请输入管理人员手机号" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitAdmininfo">确 定</el-button>
        <el-button @click="cancelSubmit">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
/**
 * 引入接口
 */
import {
  addSysEnterprise,
  delSysEnterprise,
  getSysEnterpriseInfoById, getSysEnterpriseList,
  updateSysEnterprise,
  addEnterpriseAdmin,resetEnterpriseAdminPwd,
} from "@/api/system/sysEnterprise";
import {resetPwd} from "@/api/system/sysUser";

export default {
  dicts: ['sys_enterprise_type'],

  name: "sysEnterprise_list",
  data() {
    return {
      //===========以下为新增管理员数据=================
      /**
       * 新增管理员会话框visible.sync属性值为false时会话框不显示
       */
      addAdminShow : false,

      /**
       * 新增企业管理员信息表单
       */
      enterpriseAdminInfo:{
        id:null,  //企业id
        name:null,  // 管理人员姓名
        phone:null,  // 管理人员手机号
      },
      /**
       * 新增管理员表单校验规则数据
       */
      enterpriseAdminInfoRules:{
        //校验规则中的属性名要与要校验的表单中的属性名一样
        name: [
          { required: true, message: "管理人员姓名不能为空", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "管理人员手机号不能为空", trigger: "blur" },
          { min: 11, max: 11, message: '请输入11位手机号', trigger: 'blur' }
        ],
      },
      //===========以上为新增管理员数据=================


      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        enterpriseCode: null,
        enterpriseName: null,
        enterpriseType: null,
      },
      dataList:[],  //用于存放企业对象列表数组
      total:0,
      loading:false,
      multiple:true,
      open:false,
      // 弹出层标题
      title: "",
      form:{},
      baseForm:{
        enterpriseName: undefined,
        enterpriseCode: undefined,
        enterpriseType: undefined,
        enterpriseAddress: undefined,
      },
      // 表单校验
      rules: {
        enterpriseName: [
          { required: true, message: "企业名称不能为空", trigger: "blur" }
        ],
        enterpriseCode: [
          { required: true, message: "企业代码不能为空", trigger: "blur" }
        ],
        enterpriseType: [
          { required: true, message: "企业类型不能为空", trigger: "change" }
        ]
      },
      // 选中数组
      ids: [],
    }
  },
  methods: {
    //=========以下为新增管理员相关方法===========
    /**
     * 新增管理员按钮操作
     */
    addAdmin(row){

      //重置表单内容参数为企业id
      this.resetEnterprise(row.id)
      //设置表单title
      this.title = "添加管理员";
      //修改会话框visible.sync属性值为true使会话框显示出来
      this.addAdminShow = true;
    },
    /**
     * 重置新增管理员表单方法
     * @param id
     */
    resetEnterprise(id){
      this.enterpriseAdminInfo = {
        id:id,  //将当前企业id赋值给id属性
        name:null,  // 管理人员姓名置空
        phone:null,  // 管理人员手机号置空
      };
      this.resetForm("enterpriseAdminInfo");
    },
    /**
     * 新增管理员会话框确认按钮回调函数
     */
    submitAdmininfo(){
      let own = this;
      //此处refs的属性值为要校验的组件名
      this.$refs["enterpriseAdminInfo"].validate(valid =>　{
        if (valid){
          //调用接口发送请求
          addEnterpriseAdmin(own.enterpriseAdminInfo).then( res => {
            //弹出提示添加成功对话框
            own.$modal.msgSuccess("管理员添加成功");
            //修改会话框visible.sync属性值为false，
            own.addAdminShow = false;
            //重新获取企业列表
            own.getList();
          })
        }
      })
    },
    /**
     * 新增管理员会话框取消按钮回调函数
     */
    cancelSubmit(){
      this.addAdminShow = false;
    },
    //=========以上为新增管理员相关方法===========

    //-----------以下为修改管理员密码相关方法-----------
    /**
     * 修改管理员密码按钮回调函数
     */
    alterPassword(row){
      this.$prompt('请输入新密码', "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        closeOnClickModal: false,
        inputPattern: /^.{5,20}$/,
        inputErrorMessage: "用户密码长度必须介于 5 和 20 之间"
      }).then(({value}) => {
        resetEnterpriseAdminPwd({id: row.id, pwd: value}).then(response => {
          this.$modal.msgSuccess("修改成功，新密码是：" + value);
        });
      }).catch(() => {
      });
    },


    //-----------以上为修改管理员密码相关方法-----------
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const dictId = row.id
      getSysEnterpriseInfoById(dictId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改企业类型";
      });
    },


    /** 导出按钮操作 */
    handleExport() {

    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.multiple = !selection.length
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const dictIds = row.id || this.ids;
      this.$modal.confirm('是否确认删除当前数据项？').then(function() {
        return delSysEnterprise(dictIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSysEnterprise(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSysEnterprise(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    // 表单重置
    reset() {
      this.form = _.cloneDeep(this.baseForm);
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加字典类型";
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    getList() {
      this.loading = true;
      getSysEnterpriseList(this.queryParams).then(response => {
            this.dataList = response.rows;
            this.total = response.total;
            this.loading = false;
          }
      );
    },
  },
  created() {
    this.getList();
  },
}
</script>

<style scoped>

</style>
